package com.example.exam.controller;


import com.example.exam.pojo.DTO.ExamPaperAnswerDTO.*;
import com.example.exam.pojo.DTO.QuestionDTO.QuestionAnswerDTO;
import com.example.exam.pojo.DTO.QuestionDTO.QuestionEditDTO;
import com.example.exam.pojo.Exception.ServiceException;
import com.example.exam.pojo.entity.TExamPaperAnswer;
import com.example.exam.pojo.entity.TExamPaperQuestionCustomerAnswer;
import com.example.exam.pojo.vo.ExamPaperAnswerVO.AnswerItemCorrectVO;
import com.example.exam.pojo.vo.ExamPaperVO.ExamPaperEditStandardVO;
import com.example.exam.pojo.vo.QuestionVO.QuestionStandardVO;
import com.example.exam.service.ICustomerAnswerService;
import com.example.exam.service.IExamPaperAnswerService;
import com.example.exam.service.IExamPaperService;
import com.example.exam.service.IQuestionService;
import com.example.exam.service.impl.ExamPaperAnswerImpl;
import com.example.exam.tools.JsonPage;
import com.example.exam.tools.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController("AdminExamPaperAnswerController")
@Slf4j
@Api(tags = "成绩管理模块")
@RequestMapping("/api/admin/examPaperAnswer")
public class ExamPaperAnswerController {
    @Autowired
    private IExamPaperAnswerService examPaperAnswerService;
    @Autowired
    private IExamPaperService examPaperService;

    @ApiOperation("导出成绩单")
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody PaperAnswerDTO paperAnswerDTO)throws IOException {
        log.debug("开始处理【导出已批改试卷列表】的请求,参数 {}", paperAnswerDTO );
        examPaperAnswerService.export(response,paperAnswerDTO);
//        return JsonResult.ok();
    }

    @ApiOperation("查询未批改的试卷列表")
    @PostMapping("/page")
    public JsonResult<JsonPage<TExamPaperAnswer>> pageList(@RequestBody PaperAnswerDTO paperAnswerDTO) {
        log.debug("开始处理【查询未批改的试卷列表】的请求");
        JsonPage<TExamPaperAnswer> listByPage = examPaperAnswerService.getExamPaperAnswerByPage(paperAnswerDTO);
        return JsonResult.ok(listByPage);
    }

    @ApiOperation("查询已批改的试卷列表")
    @PostMapping("/finishedPage")
    public JsonResult<JsonPage<TExamPaperAnswer>> finishedPageList(@RequestBody PaperAnswerDTO paperAnswerDTO) {
        log.debug("开始处理【查询已批改的试卷列表】的请求");
        JsonPage<TExamPaperAnswer> listByPage = examPaperAnswerService.getFinishedPaperAnswerByPage(paperAnswerDTO);
        return JsonResult.ok(listByPage);
    }

    @ApiOperation("根据答卷ID查询答卷")
    @PostMapping("/read/{id:[0-9]+}")
    public JsonResult<ExamPaperReadDTO> read (@PathVariable Integer id){
        log.debug("开始处理【根据答卷ID查询答卷】的请求");
        TExamPaperAnswer examPaperAnswer = examPaperAnswerService.selectById(id);
        ExamPaperReadDTO dto = new ExamPaperReadDTO();
        ExamPaperEditStandardVO paper = examPaperService.getEditStandardById(examPaperAnswer.getExamPaperId());
        ExamPaperSubmitDTO answer = examPaperAnswerService.getPaperAnswerById(examPaperAnswer.getId());
        dto.setPaper(paper);
        dto.setAnswer(answer);
        return JsonResult.ok(dto);
    }

    @ApiOperation("批改试卷")
    @PostMapping("/JudgePaper")
    public JsonResult JudgePaper(@RequestBody List<AnswerItemCorrectVO> answerItemCorrectVOs){
        log.debug("开始处理【批改试卷】的请求");
        log.debug("List<AnswerItemCorrectVO>="+answerItemCorrectVOs);
        Integer score = examPaperAnswerService.judge(answerItemCorrectVOs);
        return JsonResult.ok(score);
    }


}
